In [155]:
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
IMG = 'imgs/'
In [26]:
def show_img(img):
"""
Function takes an image, and shows the image using pyplot.
The image is shown in RGB
"""
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
In [111]:
img_path = os.path.join(IMG, 'sea.jpeg')
img = cv2.imread(img_path)
# What is the size of the Image?
print img.shape
# What is the class of the image?
print img.dtype
show_img(img)
In [5]:
img[50,100]
Out[5]:
In [6]:
plt.plot(img[1500,:,2])
Out[6]:
TODO: Extract a 2D slice between rows 101 to 103 and columns 201 to 203 (inclusive)
In [7]:
extraction = img[101:103,201:203]
In [8]:
cropped = img[1700:,300:,:]
In [9]:
show_img(cropped)
In [55]:
# Size of Cropped Image
cropped.shape
Out[55]:
In [36]:
img_green = img[:,:,1]
Out[36]:
In [40]:
plt.imshow(img_green, cmap='gray')
Out[40]:
In [48]:
# Load the Dog Image
dog_path = os.path.join(IMG, 'dog.jpeg')
dog = cv2.imread(dog_path)
#dog = cv2.resize
dog = cv2.resize(dog, (img.shape[1], img.shape[0]))
show_img(dog)
In [51]:
summed = dog/2 + img/2
In [52]:
show_img(summed)
In [57]:
summed_2 = (dog + img)
In [58]:
show_img(summed_2)
In [88]:
def scale(img, value):
result = img*value
return result
scaled_img = scale(img, 2)
print img[0,0,0], '\n'
print scaled_img[0,0,0]
show_img(img)
In [139]:
def blend(img_1, img_2, alpha):
#output = alpha*img_1 + (1-alpha)*img_2
#output = cv2.multiply(img_1, alpha)
#output = output.astype('int8')
#output = np.zeros((img_1.shape[0], img_2.shape[1], img_1.shape[2]))
output = cv2.addWeighted(img_1, alpha, img_2, (1-alpha), 0)
return output
In [142]:
blended_img = blend(img, dog, 0.25)
In [143]:
show_img(blended_img)
In [153]:
# grid = np.randn
# print np.random.randn.__doc__
grid = np.random.randn(100,100)
In [175]:
show_img(img)
In [183]:
grid = np.random.randn(img.shape[0], img.shape[1], img.shape[2])*10
gaussian_img = (img + grid).astype('uint8')
show_img(gaussian_img)